//
//  SEGTopicPicViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGTopicPicViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGTopicPicViewModel
-(instancetype)init{
    if (self == [super init]) {
        _topicTitle = @"";
        //        _picUrlString = @"";
    }
    return self;
}
@end

/**************************SEGTopicPicViewLayout************************/
@implementation SEGTopicPicViewLayout 
- (instancetype)initWithData:(SEGTopicPicViewModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segTopicPicViewLayout];
    return self;
}

-(void)segTopicPicViewLayout{
    //高度不固定
    _totalHeight = 0.01;
    
    //高度不固定
    //_totalHeight = 0;
    _topicTitleLabelHeight = 23;
    _topicTitleLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    _imageHeight = 93.5 * kScaleWidth;
    _imagePadding = 10;
    [self segtopicTitleLabelWithContent:[NSString stringWithFormat:@"%@",_data.topicTitle] fontSize:12 textColor:kColor_S3];
    [self segImages];
    _totalHeight += _totalImageHeight;
}

//控件topicTitleLabel文本布局计算
-(void)segtopicTitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //图片
    CGFloat padding = 3;
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_topic")];
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:image.size.height contentInsets:UIEdgeInsetsMake(0, 10, 0, padding)];
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - 20, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd..."
    
    _topicTitleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //    if (!_topicTitleLabelTextLayout)return;
    //    _topicTitleLabelHeight = [modifier heightForLineCount:_topicTitleLabelTextLayout.rowCount];
    
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    CGFloat labelWidth = w + padding + image.size.width + 20;
    _data.topicTitleWidth = labelWidth < _data.widgetWidth ? labelWidth : _data.widgetWidth;
}

- (void)setAutoHeightImageViewLayout:(SEGAutoHeightImageViewLayout *)autoHeightImageViewLayout
{
//    _autoHeightImageViewLayout = autoHeightImageViewLayout;
//    _totalHeight = _autoHeightImageViewLayout.totalHeight + 33;
//    _totalHeight += _totalImageHeight;
    _totalHeight += 43;
}

-(void)segImages{
    CGFloat imageHeight = _imageHeight;
    CGFloat imageTotalHeight = 0;

    NSUInteger imageCount = _data.picUrlStrArray.count;
    if (imageCount == 0) {
        _totalImageHeight = 0;
        return;
    }

    if (imageCount== 1) {
        imageTotalHeight = 170 * kScaleWidth;
    }

    if (imageCount> 1 && imageCount <= 3) {
        imageTotalHeight = imageHeight;
    }
    if (imageCount > 3 && imageCount <= 6) {
        imageTotalHeight = imageHeight * 2 + _imagePadding;
    }
    if (imageCount > 6 && imageCount <= 9) {
        imageTotalHeight = imageHeight * 3 + _imagePadding * 2;
    }
    _totalImageHeight = imageTotalHeight + 9;
}

@end
